﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BeatingMole
{
    /// <summary>
    /// Nguyễn Đặng Anh Duy
    /// MSSV : 11100204
    /// Email : itc.anhduy@gmail.com
    /// <summary>
    class CBusCharacter
    {
        #region "Variable"
        CInfoCharacter InfoCharacter;
        ContentManager _Content;
        bool _isEnable = true;
        bool IsReady = false;
        Vector2 PosCharacter;
        bool _isEnableBusMenu;
        int IndexSelect = 1; // Mặc định chọn người chơi đầu tiên
        #endregion

        #region "Properties"
        public bool IsEnable
        {
            set { _isEnable = value; }
            get { return _isEnable; }
        }
        public bool IsEnableBusMenu
        {
            set { _isEnableBusMenu = value; }
            get { return _isEnableBusMenu; }
        }
        #endregion

        #region "Bus"
        CBusLoadAndSaveGame BusLoadAndSaveGame;
        #endregion

        #region "Select"
        MouseState MouseCur;
        MouseState MousePre;
        float Opacity1 = 0.5f; //Sét màu cho người chơi đầu tiên 
        float Opacity2 = 1f;
        float Opacity3 = 1f;
        float Opacity4 = 1f;
        #endregion

        #region "Constructor"
        public CBusCharacter(ContentManager content)
        {
            _Content = content;
            InfoCharacter = new CInfoCharacter(_Content);
            BusLoadAndSaveGame = new CBusLoadAndSaveGame(_Content);
            PosCharacter = new Vector2(0, -480);
        }
        #endregion
        #region "Method"
        public void Update(GameTime gameTime)
        {
            if (IsEnable == true)
            {
                if (IsReady == true)
                {
                    MouseCur = Mouse.GetState();
                    if (MouseCur.LeftButton == ButtonState.Pressed && MousePre.LeftButton == ButtonState.Released)
                    {
                        #region "Event Select"
                        if (InfoCharacter.RecCharCoBa.Contains(new Point(Mouse.GetState().X, Mouse.GetState().Y)))
                        {
                            IndexSelect = 1;
                            Opacity1 = 0.5f;
                            Opacity2 = 1f;
                            Opacity3 = 1f;
                            Opacity4 = 1f;
                        }
                        if (InfoCharacter.RecCharAnhTu.Contains(new Point(Mouse.GetState().X, Mouse.GetState().Y)))
                        {
                            IndexSelect = 2;
                            Opacity1 = 1f;
                            Opacity2 = 0.5f;
                            Opacity3 = 1f;
                            Opacity4 = 1f;
                        }
                        if (InfoCharacter.RecCharBaNam.Contains(new Point(Mouse.GetState().X, Mouse.GetState().Y)))
                        {
                            IndexSelect = 3;
                            Opacity1 = 1f;
                            Opacity2 = 1f;
                            Opacity3 = 0.5f;
                            Opacity4 = 1f;
                        }
                        if (InfoCharacter.RecCharOngSau.Contains(new Point(Mouse.GetState().X, Mouse.GetState().Y)))
                        {
                            IndexSelect = 4;
                            Opacity1 = 1f;
                            Opacity2 = 1f;
                            Opacity3 = 1f;
                            Opacity4 = 0.5f;
                        }
                        #endregion
                        if (InfoCharacter.RecBtnDown.Contains(new Point(Mouse.GetState().X, Mouse.GetState().Y)))
                        {
                            IsEnableBusMenu = true;
                            IsReady = false;
                            CGlobalVariable.IsEnableMenu = true;
                            CGlobalVariable.PosMenuBack = CGlobalVariable.PositionMenu.DownBack;
                        }
                        if (InfoCharacter.RecBtnPlay.Contains(new Point(Mouse.GetState().X, Mouse.GetState().Y)))
                        {
                            CGlobalVariable.InfoPlayer = GetInfoPlayer(IndexSelect);
                            CGlobalVariable.IsStartPlay = true;
                            IsEnableBusMenu = true;
                            IsReady = false;
                        }
                        if (InfoCharacter.RecBtnLoadGame.Contains(new Point(Mouse.GetState().X, Mouse.GetState().Y)))
                        {
                            BusLoadAndSaveGame.IsEnable = true;
                            BusLoadAndSaveGame.IsBack = false;
                            IsEnable = false;
                        }
                    }
                    MousePre = MouseCur;
                }
            }
            else
            {
                BusLoadAndSaveGame.Update(gameTime);
                IsEnable = BusLoadAndSaveGame.IsBackMenu();
            }
            ManagerCharacter();
        }

        private CInfoPlayer GetInfoPlayer(int Value)
        {
            CInfoPlayer i = new CInfoPlayer(_Content);
            switch (Value)
            {
                case 1:
                    CInfoCoBa infoCoBa = new CInfoCoBa(_Content);
                    i = infoCoBa;
                    break;
                case 2:
                    CInfoAnhTu infoAnhTu = new CInfoAnhTu(_Content);
                    i = infoAnhTu;
                    break;
                case 3:
                    CInfoBaNam infoBaNam = new CInfoBaNam(_Content);
                    i = infoBaNam;
                    break;
                case 4:
                    CInfoOngSau infoOngSau = new CInfoOngSau(_Content);
                    i = infoOngSau;
                    break;
            }
            return new CInfoPlayer(_Content)
            {
                Name = i.Name,
                Bar = i.Bar,
                Avatar = i.Avatar,
                SpriteFontPlayer = i.SpriteFontPlayer,
                Score = i.Score
            };
        }

        private void ManagerCharacter()
        {
            if (IsEnableBusMenu == false)
            {
                PosCharacter.Y += CGlobalVariable.SpeedTranslate;
                if (PosCharacter.Y >= 0)
                {
                    PosCharacter.Y = 0;
                    IsReady = true;
                }
            }
            else
            {
                PosCharacter.Y -= CGlobalVariable.SpeedTranslate;
                if (PosCharacter.Y <= -480)
                {
                    PosCharacter.Y = -480;
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (IsEnable == true)
            {
                spriteBatch.Draw(InfoCharacter.BGCharacter, PosCharacter, Color.White);
                spriteBatch.Draw(InfoCharacter.BgBlack, PosCharacter, Color.Black * CGlobalVariable.Opacity);

                spriteBatch.Draw(InfoCharacter.BtnDown, new Vector2(730, 30 + PosCharacter.Y), Color.White);
                spriteBatch.Draw(InfoCharacter.BtnLoadGame, new Vector2(730, 115 + PosCharacter.Y), Color.White);
                spriteBatch.Draw(InfoCharacter.BtnPlay, new Vector2(730, 200 + PosCharacter.Y), Color.White);

                spriteBatch.Draw(InfoCharacter.CharCoBa, new Vector2(10, 10 + PosCharacter.Y), Color.White * Opacity1);
                spriteBatch.Draw(InfoCharacter.CharAnhTu, new Vector2(20 + InfoCharacter.CharCoBa.Width, 10 + PosCharacter.Y), Color.White * Opacity2);
                spriteBatch.Draw(InfoCharacter.CharBaNam, new Vector2(30 + (InfoCharacter.CharAnhTu.Width * 2), 10 + PosCharacter.Y), Color.White * Opacity3);
                spriteBatch.Draw(InfoCharacter.CharOngSau, new Vector2(40 + (InfoCharacter.CharBaNam.Width * 3), 10 + PosCharacter.Y), Color.White * Opacity4);
            }
            else
            {
                BusLoadAndSaveGame.Draw(spriteBatch);
            }
        }
        #endregion
    }
}
